<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;
use think\facade\Session;

class Ptlist extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                $where[] = ['ga.is_c', '=', 1];
                if (isset($filter['status'])) {
                    $where[] = ['ga.status', '=', $filter['status']];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['start_time'])) {
                    $create_time = explode(' - ', $filter['start_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['ga.start_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['end_time'])) {
                    $end_time = explode(' - ', $filter['end_time']);
                    foreach ($end_time as $k => $v) {
                        $end_time[$k] = strtotime($v);
                    }
                    $where[] = ['ga.end_time', 'between', [$end_time[0], $end_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('bargain_activity')->alias('ga')
                ->join('member m', 'ga.member_id=m.id')
                ->field('ga.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('ga.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['success_time']) {
                    $_list[$k]['success_time'] = date('Y-m-d H:i:s', $v['success_time']);
                } else {
                    $_list[$k]['success_time'] = "------";
                }
            }
            //获取数量
            $total = Db::name('bargain_activity')->alias('ga')
                ->join('member m', 'ga.member_id=m.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    //排队订单列表
    public function porder()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['activity_id', '=', $id];
            $filter = input('filter');
            if ($filter) {

            }
            $_list = Db::name('bargain_record')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
//                $_list[$k]['order_info']=Db::name('order')->find($v['order_id']);
                $_list[$k]['member_info']=Db::name('member')->find($v['member_id']);
            }
            //获取数量
            $total = Db::name('bargain_record')
                ->where($where)   //0待审核 3审核失败
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'id' => $id,
        ]);
        return $this->fetch();
    }
    public function goods_list(){
        $id = $this->request->param('id/d', 0);
        $goods_list = Db::name('order_goods')
            ->where(['order_id' => $id])
            ->order('sort desc')
            ->select();
        foreach ($goods_list as $k => $v) {
            $goods_info=Db::name('goods')->find($v['goods_id']);
            $goods_list[$k]['goods_info']=$goods_info;
        }
        $this->assign([
            'goods_list' => $goods_list
        ]);
        return $this->fetch('server');
    }
}